// void, Obj me

Tower this;

Sleep(rand(100)+30);
this = me.AsTower;

while (.IsValid)
{
	Unit target;
	if (.settlement.IsOutpost() && .settlement.IsIndependent)
	{
			Sleep(500);
			return;
	}
	target = .GetUITarget.AsUnit;
	if (target.IsAlive)
	if (.IsValidTarget(target))
	if (.CanAttack(target))
	{
		bool bAttacked = false;
		while (.Attack(target))
		{
			//######### MODUL GUARDIAN 1 START ###########
			if(target.AsUnit.IsHeirOf("EGuardian")){
				if(target.AsUnit.command != "engage" &&
				   target.AsUnit.command != "engage_unit_type"){
					if(rand(100) > 10){
						if(!target.AsUnit.BestTargetForPos.IsValid)
						if(target.AsUnit.health < target.AsUnit.maxhealth){
							target.AsUnit.Heal(500);
						}
					}
				}
			}
			//######### MODUL GUARDIAN 1 END ###########
			bAttacked = true;
			Sleep(100);
			if (!.IsValid) return;
			if (!target.IsAlive) break;
		}
		Sleep(500);
		if (bAttacked) continue;
	}
//	pr("Looking for best target");
	target = .BestTarget;
	if (!target.IsAlive) {
//		pr("Best target not found, sleeping for 1000");
		Sleep(1000); 
		continue;
	}
//	pr("Trying to attack enemy");
	while (.Attack(target)) {
//		pr("FireTower::Guard - attacking");
		Sleep(100);
		if (!.IsValid) return;
		if (!target.IsValid) break;
		if (!target.IsAlive) break;
		if (.DistTo(target) < .min_range) break;
		
		//######### MODUL GUARDIAN 1 START ###########
		if(target.AsUnit.IsHeirOf("EGuardian")){
			if(target.AsUnit.command != "engage" &&
			   target.AsUnit.command != "engage_unit_type"){
				if(rand(100) > 10){
					if(!target.AsUnit.BestTargetForPos.IsValid)
					if(target.AsUnit.health < target.AsUnit.maxhealth){
						target.AsUnit.Heal(500);
					}
				}
			}
		}
		//######### MODUL GUARDIAN 1 END ###########
	}
	Sleep(500);
}
